package ru.royalgatemagazine.app.view;

import ru.royalgatemagazine.app.Debug;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class AbstractView extends FrameLayout {
	
	public AbstractView(Context context) {
		super(context);
		LayoutInflater.from(context).inflate(layout(), this);
	}
	
	public AbstractView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(layout(), this);
	}

	public AbstractView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		LayoutInflater.from(context).inflate(layout(), this);
	}
	
	protected abstract int layout();
	
	public void setViewBackground(int id, int res){
		View v = findViewById(id);
		if(v!=null){
			v.setBackgroundResource(res);
		}
		else{
			Debug.log("setViewBackground(int id, int res), v==null, res_id: "+res);
		}
	}
	
	public void setViewBackground(int id, Drawable d){
		View v = findViewById(id);
		if(v!=null){
			v.setBackgroundDrawable(d);
		}
		else{
			Debug.log("setViewBackground(int id, Drawable d), v==null");
		}
	}
	
	public void setImageResource(int id, int res){
		ImageView v = (ImageView) findViewById(id);
		if(v!=null){
			v.setImageResource(res);
		}
		else{
			Debug.log("setImageResource(int id, int res), v==null, res_id: "+res);
		}
	}
	
	public void setImageDrawable(int id, Drawable d){
		ImageView v = (ImageView) findViewById(id);
		if(v!=null){
			v.setImageDrawable(d);
		}
		else{
			Debug.log("setImageResource(int id, Drawable d), v==null");
		}
	}
	
	public void setText(int id, int res){
		TextView v = (TextView) findViewById(id);
		if(v!=null){
			v.setText(res);
		}
		else{
			Debug.log("setText(), v==null, res_id: "+res);
		}
	}
	
	public void setText(int id, String text){
		TextView v = (TextView) findViewById(id);
		if(v!=null && text!=null){
			v.setText(text);
		}
		else{
			Debug.log("setText(), v==null || string==null, string: "+text);
		}
	}
	
	public static class Helper{
		
		public static void setViewBackground(View view, int id, int res){
			View v = view.findViewById(id);
			if(v!=null){
				v.setBackgroundResource(res);
			}
			else{
				Debug.log("setViewBackground(int id, int res), v==null, res_id: "+res);
			}
		}
		
		public static void setViewBackground(View view, int id, Drawable d){
			View v = view.findViewById(id);
			if(v!=null){
				v.setBackgroundDrawable(d);
			}
			else{
				Debug.log("setViewBackground(int id, Drawable d), v==null");
			}
		}
		
		public static void setImageResource(View view, int id, int res){
			ImageView v = (ImageView) view.findViewById(id);
			if(v!=null){
				v.setImageResource(res);
			}
			else{
				Debug.log("setImageResource(int id, int res), v==null, res_id: "+res);
			}
		}
		
		public static void setImageDrawable(View view, int id, Drawable d){
			ImageView v = (ImageView) view.findViewById(id);
			if(v!=null){
				v.setImageDrawable(d);
			}
			else{
				Debug.log("setImageResource(int id, Drawable d), v==null");
			}
		}
		
		public static void setText(View view, int id, int res){
			TextView v = (TextView) view.findViewById(id);
			if(v!=null){
				v.setText(res);
			}
			else{
				Debug.log("setText(), v==null, res_id: "+res);
			}
		}
		
		public static void setText(View view, int id, String text){
			TextView v = (TextView) view.findViewById(id);
			if(v!=null && text!=null){
				v.setText(text);
			}
			else{
				Debug.log("setText(), v==null || string==null, string: "+text);
			}
		}
	}
}
